<?php
/**
 *  verify.func.php 验证函数库
 * 
 * @version    0.1
 * @author    Hyperblue   i@kushu.net
 * @pubdate    2011-08-25 13:50
 */


/**
 * 检查用户名是否符合规定(2-20位字母数字汉字以及下划线)
 *
 * @param   string   $username   要检查的用户名
 * @return   boolean
 */
function V_username($username) {
	$strlen = strlen($username);
	if(V_badword($username) || !preg_match("/^[a-zA-Z0-9_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]+$/", $username)){
		return false;
	} elseif ( 20 <= $strlen || $strlen < 2 ) {
		return false;
	}
	return true;
}

/**
 * 检查密码长度是否符合规定(6-20位)
 *
 * @param   string   $password
 * @return   boolean
 */
function V_password($password) {
	$strlen = strlen($password);
	if($strlen >= 6 && $strlen <= 20) return true;
	return false;
}

/**
 * 验证输入的邮件地址是否合法
 *
 * @param   string   $email   需要验证的邮件地址
 * @return   boolean
 */
function V_email($email)
{
    $partn = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";
    if (strpos($email, '@') !== false && strpos($email, '.') !== false) {
        if (preg_match($partn, $email)) return true;
        else return false;
    } else {
        return false;
    }
}

/**
 * 判断移动手机格式是否正确
 *
 * @param   $phone
 * @return   boolean
 */
function V_phone($phone) {
    return strlen($phone) == 11 && preg_match("/^1[0-9]{10}$/", $phone);
}

/**
 * 判断QQ格式是否正确
 *
 * @param   $qq
 * @return   boolean
 */
function V_qq($qq) {
    return strlen($qq) > 4 && preg_match("/^[1-9]\d{4,12}$/", $qq);
}

/*
 * 身份证号码验证
 * @param   $id_card 
 * @param   $length
 * @return   boolean
 */
function V_idcard($id_card, $length = 'both') {
    if(strlen($id_card) == 15 && $length == 'both') { //当$length不等于'both'时，15位号码无效
        $truenum = substr($id_card, 0, 6) . '19' . substr($id_card, 6); //为返回18位号码作准备。
        $preg = "/^[\d]{8}((0[1-9])|(1[0-2]))((0[1-9])|([12][\d])|(3[01]))[\d]{3}$/";
    } elseif (strlen($id_card) == 18) {
        $truenum = substr($id_card, 0, 17);
        $preg = "/^[\d]{6}((19[\d]{2})|(200[0-8]))((0[1-9])|(1[0-2]))((0[1-9])|([12][\d])|(3[01]))[\d]{3}[0-9xX]$/";
    } else {
        return false;
    }
    if (!preg_match($preg, $id_card))return false;
	
    $nsum = substr($truenum, 0, 1) * 7;
    $nsum = $nsum + substr($truenum, 1, 1) * 9;
    $nsum = $nsum + substr($truenum, 2, 1) * 10;
    $nsum = $nsum + substr($truenum, 3, 1) * 5;
    $nsum = $nsum + substr($truenum, 4, 1) * 8;
    $nsum = $nsum + substr($truenum, 5, 1) * 4;
    $nsum = $nsum + substr($truenum, 6, 1) * 2;
    $nsum = $nsum + substr($truenum, 7, 1) * 1;
    $nsum = $nsum + substr($truenum, 8, 1) * 6;
    $nsum = $nsum + substr($truenum, 9, 1) * 3;
    $nsum = $nsum + substr($truenum, 10, 1) * 7;
    $nsum = $nsum + substr($truenum, 11, 1) * 9;
    $nsum = $nsum + substr($truenum, 12, 1) * 10;
    $nsum = $nsum + substr($truenum, 13, 1) * 5;
    $nsum = $nsum + substr($truenum, 14, 1) * 8;
    $nsum = $nsum + substr($truenum, 15, 1) * 4;
    $nsum = $nsum + substr($truenum, 16, 1) * 2;
	
    $yzm = 12 - $nsum % 11;
    if ($yzm == 10) $yzm = 'x';
    elseif ($yzm == 12) $yzm = '1';
    elseif ($yzm == 11) $yzm = '0';
	
    /* ----------18位验证码计算完成------------- */
    if (strlen($id_card) == 18) {
        if (strtolower(substr($id_card, 17, 1)) != $yzm)
            return false;
    }
    return true;
}

/**
 * 检查是否含有系统限制的非法字符
 *
 * @param   string   $string
 * @return   boolean
 */
function V_badword($string) {
	return false;
}

/**
 * 检查是否为一个合法的时间格式
 *
 * @param   string   $time
 * @return   boolean
 */
function V_time($time) {
    $pattern = '/[\d]{4}-[\d]{1,2}-[\d]{1,2}\s[\d]{1,2}:[\d]{1,2}:[\d]{1,2}/';

    return preg_match($pattern, $time);
}

/**
 * IE浏览器判断
 *
 * @return   boolean
 */
function V_ie() {
    $useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
    if ((strpos($useragent, 'opera') !== false) || (strpos($useragent, 'konqueror') !== false))
        return false;
    if (strpos($useragent, 'msie ') !== false)
        return true;
    return false;
}

/**
 * 检查是否是引擎爬虫和机器人访问网站
 *
 * @return   boolean
 */
function V_robot(){
	static $is_robot = null;
	
	if(null == $is_robot){
		$is_robot = false;
		$robotlist = 'bot|spider|crawl|nutch|lycos|robozilla|slurp|search|seek|archive';
		if( isset($_SERVER['HTTP_USER_AGENT']) && preg_match("/{$robotlist}/i", $_SERVER['HTTP_USER_AGENT']) ){
			$is_robot = true;
		}
	}
	
	return $is_robot;
	
}

/**
 * 判断是否为手持设别的函数
 *
 * @return   boolean
 */
function V_mobile() {

    $devices = array(
        "operaMobi" => "Opera Mobi",
        "android" => "android",
        "blackberry" => "blackberry",
        "iphone" => "(iphone|ipod)",
        "opera" => "opera mini",
        "palm" => "(avantgo|blazer|elaine|hiptop|palm|plucker|xiino)",
        "windows" => "windows ce; (iemobile|ppc|smartphone)",
        "generic" => "(kindle|mobile|mmp|midp|o2|pda|pocket|psp|symbian|smartphone|treo|up.browser|up.link|vodafone|wap)"
    );
    
    if (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])) {
        return TRUE;
    } elseif (isset($_SERVER['HTTP_ACCEPT']) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/vnd.wap.wml') > 0 || strpos($_SERVER['HTTP_ACCEPT'], 'application/vnd.wap.xhtml+xml') > 0)) {
        return TRUE;
    } else {
        
        if (isset($_SERVER['HTTP_USER_AGENT'])) {
            foreach ($devices as $device => $regexp) {
                
                if (preg_match("/" . $regexp . "/i", $_SERVER['HTTP_USER_AGENT'])) {
                    return TRUE;
                }
            }
        }
    }
    return FALSE;
}

/**
 * 判断字符串是否为utf8编码，英文和半角字符返回ture
 *
 * @param   $string
 * @return  boolean
 */
function V_utf8($string) {
	return preg_match('%^(?:
		[\x09\x0A\x0D\x20-\x7E] # ASCII
		| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
		| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
		| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
		| \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
		| \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
		| [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
		| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
		)*$%xs', $string);
}
